﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DnaCoreLib;

namespace DnaServiceLib
{
    public class MenuManager
    {
        private List<MenuItem> menuList=new List<MenuItem>();

        public MenuManager()
        {
        }

        public bool LoadFromPluginManager(PluginManager pluginManager)
        {
            menuList.Clear();
            foreach (IFileImporter list in pluginManager.FileImporterList)
                AddMenuItem(list.MenuItem);
            foreach (IWebImporter list in pluginManager.WebImporterList)
                AddMenuItem(list.MenuItem);
            foreach (IAnalyzer list in pluginManager.AnalyzerList)
                AddMenuItem(list.MenuItem);
            foreach (IImportConfiguration list in pluginManager.ImportConfigurationList)
                AddMenuItem(list.MenuItem);
            return true;

        }

        public List<MenuItem> MenuItemList
        {
            get
            {
                return menuList;
            }
        }

        public void AddMenuItem(MenuItem menuItem)
        {
            if (menuList.Where(w=>w==menuItem).FirstOrDefault()==null)
                menuList.Add(menuItem);
        }

        public void AddMenuItemRange(MenuItem[] menuItems)
        {
            //Add where not already in the menu
            menuList.AddRange(menuItems.Where(w=>!menuList.Contains(w)));
        }

        public MenuApplication[] AppList
        {
            get
            {
                List<MenuApplication> list = new List<MenuApplication>();
                var groupedResults1 = menuList.GroupBy(g => g.App);
                foreach (var gp in groupedResults1)
                {
                    list.Add(new MenuApplication(gp.Key));
                }
                return list.ToArray();
            }
        }

        public MenuCategory[] CategoryList(string app)
        {
                List<MenuCategory> list = new List<MenuCategory>();
                var groupedResults1 = menuList.Where(g=>g.App==app).GroupBy(g => new {g.App, g.Category});
                foreach (var gp in groupedResults1)
                {
                    list.Add(new MenuCategory(gp.Key.App, gp.Key.Category));
                }
                return list.ToArray();
        }

        public MenuItem[] FunctionList(string app, string category)
        {
                return menuList.Where(w=>w.App==app && w.Category==category).ToArray();
        }

    }
}
